<template>
  <view class="house-list-container">
    <!-- 搜索栏 -->
    <view class="search-bar">
      <view class="search-input-wrapper">
        <input 
          v-model="searchKeyword" 
          class="search-input" 
          placeholder="搜索房源信息"
          @input="handleSearch"
        />
        <text class="search-icon">🔍</text>
      </view>
    </view>

    <!-- 筛选条件 -->
    <view class="filter-bar">
      <view class="filter-item" @click="showStatusFilter = !showStatusFilter">
        <text class="filter-label">状态</text>
        <text class="filter-value">{{ getStatusText(selectedStatus) }}</text>
        <text class="filter-arrow">▼</text>
      </view>
      <view class="filter-item" @click="showTypeFilter = !showTypeFilter">
        <text class="filter-label">类型</text>
        <text class="filter-value">{{ getTypeText(selectedType) }}</text>
        <text class="filter-arrow">▼</text>
      </view>
    </view>

    <!-- 状态筛选弹窗 -->
    <view v-if="showStatusFilter" class="filter-popup">
      <view class="filter-options">
        <view 
          v-for="status in statusOptions" 
          :key="status.value"
          class="filter-option"
          :class="{ active: selectedStatus === status.value }"
          @click="selectStatus(status.value)"
        >
          {{ status.label }}
        </view>
      </view>
    </view>

    <!-- 类型筛选弹窗 -->
    <view v-if="showTypeFilter" class="filter-popup">
      <view class="filter-options">
        <view 
          v-for="type in typeOptions" 
          :key="type.value"
          class="filter-option"
          :class="{ active: selectedType === type.value }"
          @click="selectType(type.value)"
        >
          {{ type.label }}
        </view>
      </view>
    </view>

    <!-- 房源列表 -->
    <view class="house-list">
      <view 
        v-for="house in houseList" 
        :key="house.id"
        class="house-item"
        @click="viewHouseDetail(house)"
      >
        <view class="house-header">
          <text class="house-num">{{ house.houseNum }}</text>
          <view class="house-status" :class="getStatusClass(house.status)">
            {{ getStatusText(house.status) }}
          </view>
        </view>
        
        <view class="house-info">
          <text class="house-address">{{ house.address }}</text>
          <text class="house-cell">{{ house.cellName }}</text>
        </view>
        
        <view class="house-details">
          <view class="detail-item">
            <text class="detail-label">业主：</text>
            <text class="detail-value">{{ house.ownerName }}</text>
          </view>
          <view class="detail-item">
            <text class="detail-label">联系方式：</text>
            <text class="detail-value">{{ house.ownerPhone }}</text>
          </view>
          <view class="detail-item">
            <text class="detail-label">租金：</text>
            <text class="detail-value price">¥{{ house.price }}/月</text>
          </view>
        </view>
        
        <view v-if="house.remark" class="house-remark">
          <text class="remark-text">{{ house.remark }}</text>
        </view>
      </view>
    </view>

    <!-- 加载更多 -->
    <view v-if="hasMore" class="load-more" @click="loadMore">
      <text class="load-more-text">加载更多</text>
    </view>

    <!-- 空状态 -->
    <view v-if="!loading && houseList.length === 0" class="empty-state">
      <text class="empty-icon">🏠</text>
      <text class="empty-text">暂无房源信息</text>
    </view>

    <!-- 加载状态 -->
    <view v-if="loading" class="loading-state">
      <text class="loading-text">加载中...</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';
import { queryHouseList } from '@/api/house';
import type { HouseListItem, QueryHouseListReq } from '@/api/house/types';

// 响应式数据
const searchKeyword = ref('');
const selectedStatus = ref(0); // 0-全部，1-已出租，2-待出租
const selectedType = ref(0); // 0-全部，1-整租，2-合租，3-其他
const showStatusFilter = ref(false);
const showTypeFilter = ref(false);
const houseList = ref<HouseListItem[]>([]);
const loading = ref(false);
const hasMore = ref(true);
const currentPage = ref(0);
const pageSize = ref(10);

// 筛选选项
const statusOptions = [
  { label: '全部', value: 0 },
  { label: '已出租', value: 1 },
  { label: '待出租', value: 2 }
];

const typeOptions = [
  { label: '全部', value: 0 },
  { label: '整租', value: 1 },
  { label: '合租', value: 2 },
  { label: '其他', value: 3 }
];

// 计算属性
const getStatusText = (status: number) => {
  const option = statusOptions.find(item => item.value === status);
  return option ? option.label : '未知';
};

const getTypeText = (type: number) => {
  const option = typeOptions.find(item => item.value === type);
  return option ? option.label : '未知';
};

const getStatusClass = (status: number) => {
  return {
    'status-rented': status === 1,
    'status-available': status === 2
  };
};

// 方法
const loadHouseList = async (reset = false) => {
  if (loading.value) return;
  
  loading.value = true;
  
  try {
    const params: QueryHouseListReq = {
      page: reset ? 0 : currentPage.value,
      limit: pageSize.value,
      tenantId: 0,
      name: searchKeyword.value
    };
    
    const response = await queryHouseList(params);
    
    if (response.code === 0) {
      const newList = response.data.list.filter(house => {
        // 状态筛选
        if (selectedStatus.value !== 0 && house.status !== selectedStatus.value) {
          return false;
        }
        // 类型筛选
        if (selectedType.value !== 0 && house.type !== selectedType.value) {
          return false;
        }
        return true;
      });
      
      if (reset) {
        houseList.value = newList;
        currentPage.value = 0;
      } else {
        houseList.value.push(...newList);
      }
      
      hasMore.value = response.data.hasNextPage;
      currentPage.value = response.data.pageNum;
    } else {
      uni.showToast({
        title: response.message || '加载失败',
        icon: 'none'
      });
    }
  } catch (error) {
    console.error('加载房源列表失败:', error);
    uni.showToast({
      title: '加载失败',
      icon: 'none'
    });
  } finally {
    loading.value = false;
  }
};

const handleSearch = () => {
  loadHouseList(true);
};

const selectStatus = (status: number) => {
  selectedStatus.value = status;
  showStatusFilter.value = false;
  loadHouseList(true);
};

const selectType = (type: number) => {
  selectedType.value = type;
  showTypeFilter.value = false;
  loadHouseList(true);
};

const loadMore = () => {
  if (hasMore.value && !loading.value) {
    loadHouseList();
  }
};

const viewHouseDetail = (house: HouseListItem) => {
  uni.navigateTo({
    url: `/pages/common/house-detail/index?id=${house.houseId}`
  });
};

// 生命周期
onMounted(() => {
  loadHouseList(true);
});
</script>

<style lang="scss" scoped>
.house-list-container {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20rpx;
}

.search-bar {
  background: white;
  border-radius: 12rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.search-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.search-input {
  flex: 1;
  height: 60rpx;
  padding: 0 50rpx 0 20rpx;
  border: 2rpx solid #e0e0e0;
  border-radius: 30rpx;
  font-size: 28rpx;
  background: #f8f8f8;
}

.search-icon {
  position: absolute;
  right: 20rpx;
  font-size: 28rpx;
  color: #999;
}

.filter-bar {
  display: flex;
  background: white;
  border-radius: 12rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.filter-item {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 30rpx 20rpx;
  border-right: 1rpx solid #e0e0e0;
  
  &:last-child {
    border-right: none;
  }
}

.filter-label {
  font-size: 28rpx;
  color: #333;
  margin-right: 10rpx;
}

.filter-value {
  font-size: 26rpx;
  color: #666;
  margin-right: 8rpx;
}

.filter-arrow {
  font-size: 20rpx;
  color: #999;
}

.filter-popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: flex;
  align-items: flex-end;
}

.filter-options {
  width: 100%;
  background: white;
  border-radius: 20rpx 20rpx 0 0;
  padding: 40rpx 0;
}

.filter-option {
  padding: 30rpx 40rpx;
  font-size: 28rpx;
  color: #333;
  border-bottom: 1rpx solid #f0f0f0;
  
  &.active {
    color: #007aff;
    background: #f0f8ff;
  }
  
  &:last-child {
    border-bottom: none;
  }
}

.house-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.house-item {
  background: white;
  border-radius: 12rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.house-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.house-num {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.house-status {
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-size: 22rpx;
  color: white;
  
  &.status-rented {
    background: #ff4757;
  }
  
  &.status-available {
    background: #2ed573;
  }
}

.house-info {
  margin-bottom: 20rpx;
}

.house-address {
  font-size: 28rpx;
  color: #333;
  display: block;
  margin-bottom: 8rpx;
}

.house-cell {
  font-size: 24rpx;
  color: #666;
}

.house-details {
  display: flex;
  flex-direction: column;
  gap: 12rpx;
  margin-bottom: 20rpx;
}

.detail-item {
  display: flex;
  align-items: center;
}

.detail-label {
  font-size: 24rpx;
  color: #666;
  width: 140rpx;
}

.detail-value {
  font-size: 24rpx;
  color: #333;
  
  &.price {
    color: #ff4757;
    font-weight: bold;
  }
}

.house-remark {
  padding: 20rpx;
  background: #f8f9fa;
  border-radius: 8rpx;
  border-left: 4rpx solid #007aff;
}

.remark-text {
  font-size: 24rpx;
  color: #666;
  line-height: 1.5;
}

.load-more {
  text-align: center;
  padding: 40rpx;
}

.load-more-text {
  font-size: 28rpx;
  color: #007aff;
}

.empty-state {
  text-align: center;
  padding: 100rpx 40rpx;
}

.empty-icon {
  font-size: 80rpx;
  display: block;
  margin-bottom: 20rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}

.loading-state {
  text-align: center;
  padding: 40rpx;
}

.loading-text {
  font-size: 28rpx;
  color: #666;
}
</style>
